package com.hk.webapp.interceptors;

import com.hk.webapp.res_config.JwtAudience;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * (C) Copyright 2017-now
 * All rights reserved.
 * <p>
 * 定义自拦截器配置
 *
 * @author DreamerCK
 * @date 2019-01-11 11:51
 **/
@Configuration
public class CustomInterceptorsConfig implements WebMvcConfigurer {

    @Resource
    private JwtAudience audience;

    @Bean(name = "adminJwtInterceptor")
    JwtInterceptor adminJwtInterceptor() {
        return new JwtInterceptor(audience.getAdminSecretKey());
    }

    @Bean(name = "clientJwtInterceptor")
    JwtInterceptor clientJwtInterceptor() {
        return new JwtInterceptor(audience.getClientSecretKey());
    }

    @Bean
    AuthInterceptor authInterceptor() {
        return new AuthInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(clientJwtInterceptor()).addPathPatterns("/api/**").excludePathPatterns("/api/admin/**");
        registry.addInterceptor(adminJwtInterceptor()).addPathPatterns("/api/admin/**");
        registry.addInterceptor(authInterceptor()).addPathPatterns("/api/admin/**");
    }

}
